package org.komarichin.jpa.entity;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 20.09.2009
 * Time: 17:21:21
 */
@Entity(name = "User")
@Table(name = "USER")
@NamedQueries({
        @NamedQuery(name = "user.findAll", query = "select u from User u"),
        @NamedQuery(name = "user.login", query = "select u from User u where u.login = :login and u.password = :password"),
        @NamedQuery(name = "user.forgotPassword", query = "select u from User u where u.login = :login and u.hint = :hint"),
        @NamedQuery(name = "user.findByLogin", query = "select u from User u where u.login = :login"),
        @NamedQuery(name = "user.findAllowUsers", query = "select u from User u, Role r where r.id = u.role and r.permission <= :permission")
})
public class User implements IUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String login;
    private String password;
    private String hint;
    @Temporal(TemporalType.TIMESTAMP)
    private Date registration;
    @Column(name = "last_login")
    private Date lastLogin;
    private Long role;


    /**
     * {@inheritDoc}
     */
    public Long getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    public void setId(Long id) {
        this.id = id;
    }
//
//    public Key getId() {
//        return id;
//    }
//
//    public void setId(Key id) {
//        this.id = id;
//    }

    /**
     * {@inheritDoc}
     */
    public String getLogin() {
        return login;
    }

    /**
     * {@inheritDoc}
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * {@inheritDoc}
     */
    public String getPassword() {
        return password;
    }

    /**
     * {@inheritDoc}
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * {@inheritDoc}
     */
    public Date getRegistration() {
        return registration;
    }

    /**
     * {@inheritDoc}
     */
    public void setRegistration(Date registration) {
        this.registration = registration;
    }

    /**
     * {@inheritDoc}
     */
    public Date getLastLogin() {
        return lastLogin;
    }

    /**
     * {@inheritDoc}
     */
    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    /**
     * {@inheritDoc}
     */
    public Long getRole() {
        return role;
    }

    /**
     * {@inheritDoc}
     */
    public void setRole(Long role) {
        this.role = role;
    }

    /**
     * {@inheritDoc}
     */
    public String getHint() {
        return hint;
    }

    /**
     * {@inheritDoc}
     */
    public void setHint(String hint) {
        this.hint = hint;
    }

//    /**
//     * {@inheritDoc}
//     */
//    @Override
//    public Key getKey() {
//        return id;
//    }
//
//    /**
//     * {@inheritDoc}
//     */
//    @Override
//    public void setKey(Key key) {
//        this.id = key;
//    }

    @Override
    @Transient
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("User");
        sb.append("{id=").append(id);
        sb.append(", login='").append(login).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", hint='").append(hint).append('\'');
        sb.append(", registration=").append(registration);
        sb.append(", lastLogin=").append(lastLogin);
        sb.append('}');
        return sb.toString();
    }

    @Override
    @Transient
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;

        User user = (User) o;

        if (hint != null ? !hint.equals(user.hint) : user.hint != null) return false;
        if (id != null ? !id.equals(user.id) : user.id != null) return false;
        if (lastLogin != null ? !lastLogin.equals(user.lastLogin) : user.lastLogin != null) return false;
        if (login != null ? !login.equals(user.login) : user.login != null) return false;
        if (password != null ? !password.equals(user.password) : user.password != null) return false;
        if (registration != null ? !registration.equals(user.registration) : user.registration != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (login != null ? login.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (hint != null ? hint.hashCode() : 0);
        result = 31 * result + (registration != null ? registration.hashCode() : 0);
        result = 31 * result + (lastLogin != null ? lastLogin.hashCode() : 0);
        return result;
    }
}
